<html>
<head><meta charset="utf-8"><title>String::some() to return Option&lt;String&gt; · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html">String::some() to return Option&lt;String&gt;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206897577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/206897577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#206897577">(Aug 14 2020 at 04:37)</a>:</h4>
<p>Would it be good to have <code>String::some()</code> to return <code>Option&lt;String&gt;</code> if it is not empty for feature parity with <code>Result::some()</code>? Do we need a tracking issue for this?</p>



<a name="206897810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/206897810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#206897810">(Aug 14 2020 at 04:42)</a>:</h4>
<p>The empty string is a valid string, so calling it <code>String::some()</code> feels like a bad name. Perhaps a name like <code>String::non_empty</code>?</p>



<a name="206900383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/206900383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#206900383">(Aug 14 2020 at 05:47)</a>:</h4>
<p><span class="user-mention" data-user-id="225422">@pickfire</span> I'm <a href="https://doc.rust-lang.org/nightly/std/result/enum.Result.html">not seeing</a> a <code>Result::some</code>, so I don't follow what you're suggesting.  Also, would you expect it on <code>Vec</code> as well?</p>
<p>And it's not obvious to me that such a method is generally-useful enough to be worth having.  It's not <code>Option&lt;NonEmptyString&gt;</code>, so the bool from <code>!.is_empty()</code> seems fine.  And there's always the <code>Some(string).filter(|x| !x.is_empty())</code> dance available...</p>



<a name="206904939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/206904939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#206904939">(Aug 14 2020 at 07:25)</a>:</h4>
<p>or <code>(!string.is_empty()).then_some(string)</code></p>



<a name="206943372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/206943372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#206943372">(Aug 14 2020 at 15:45)</a>:</h4>
<p><span class="user-mention" data-user-id="225422">@pickfire</span> Under what circumstances, or for what use cases, would you want this?</p>



<a name="206973683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/206973683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#206973683">(Aug 14 2020 at 19:55)</a>:</h4>
<p>Feels a lot like a form of validation.</p>



<a name="207024453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/207024453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#207024453">(Aug 15 2020 at 14:35)</a>:</h4>
<p>An alias for <code>if vec.is_empty() { None } else { Some(vec) }</code>.</p>



<a name="207024498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/207024498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#207024498">(Aug 15 2020 at 14:36)</a>:</h4>
<p>Wait, I mean <code>Result::ok()</code>, not <code>Result::some()</code>.</p>



<a name="207024572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/207024572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#207024572">(Aug 15 2020 at 14:38)</a>:</h4>
<p>Rather than doing it in 6 lines, one can do it in one.</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">completions</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">build</span><span class="p">();</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="n">completions</span><span class="p">.</span><span class="n">is_empty</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">None</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Some</span><span class="p">(</span><span class="n">completions</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Vs</p>
<div class="codehilite"><pre><span></span><code><span class="n">builder</span><span class="p">.</span><span class="n">build</span><span class="p">().</span><span class="n">some</span><span class="p">()</span><span class="w">  </span><span class="c1">// or non_empty()</span>
</code></pre></div>


<p>But personally I wouldn't have guessed that <code>non_empty</code> would returns <code>Option</code>.</p>



<a name="207025475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/207025475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Kalbertodt <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#207025475">(Aug 15 2020 at 15:05)</a>:</h4>
<p>Maybe <code>non_empty_or_none</code> is more clear on what it returns.</p>



<a name="207051253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/207051253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#207051253">(Aug 16 2020 at 02:47)</a>:</h4>
<p><span class="user-mention" data-user-id="225422">@pickfire</span> note that <code>Result::ok</code> (and <code>Result::err</code>) are methods with names matching the variants of the enum, so don't seem like obvious precedents for this.  And cuviper and I have each provided existing one-liners for this.</p>



<a name="207066899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Asome%28%29%20to%20return%20Option%3CString%3E/near/207066899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Asome().20to.20return.20Option.3CString.3E.html#207066899">(Aug 16 2020 at 11:15)</a>:</h4>
<p>Yes, they looks like a hack to me, I mean not as straightforward to understand.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>